package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.MallPeripherySpecificationConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallPeripherySpecificationDtoMapper;
import com.starlink.model.domain.MallPeripherySpecificationDO;
import com.starlink.mapper.MallPeripherySpecificationMapper;
import com.starlink.model.request.MallPeripherySpecificationRequest;
import com.starlink.service.MallPeripherySpecificationService;
import com.starlink.model.vo.MallPeripherySpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;
import java.util.Map;

/**
 * @author zhanghong
 */
@Service
public class MallPeripherySpecificationServiceImpl extends ServiceImpl<MallPeripherySpecificationMapper, MallPeripherySpecificationDO> implements MallPeripherySpecificationService {
    @Autowired
    private MallPeripherySpecificationMapper mallPeripherySpecificationMapper;

    @Override
    public MallPeripherySpecificationVO getByKey(long id) {
        MallPeripherySpecificationVO mallPeripherySpecificationVO = MallPeripherySpecificationDtoMapper.INSTANCE.do2VO(mallPeripherySpecificationMapper.selectDOById(id));
        return mallPeripherySpecificationVO;
    }

    @Override
    public PageInfo<MallPeripherySpecificationVO> pageQuery(Pageable pageable,MallPeripherySpecificationRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallPeripherySpecificationDO> list = mallPeripherySpecificationMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallPeripherySpecificationVO> mallPeripherySpecificationVOList = MallPeripherySpecificationConvert.buildMallPeripherySpecificationVoList(list);
        return new PageInfo(mallPeripherySpecificationVOList);
    }

    @Override
    public List<MallPeripherySpecificationVO> listQuery(MallPeripherySpecificationRequest request) {
        List<MallPeripherySpecificationDO> list = mallPeripherySpecificationMapper.selectDOList(MallPeripherySpecificationDtoMapper.INSTANCE.request2DO(request));
        List<MallPeripherySpecificationVO> mallPeripherySpecificationVOS = MallPeripherySpecificationConvert.buildMallPeripherySpecificationVoList(list);
        return mallPeripherySpecificationVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallPeripherySpecificationRequest request) {
        request.setId(getId());
        save(MallPeripherySpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallPeripherySpecificationRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallPeripherySpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallPeripherySpecificationMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public void saveBatch(List<MallPeripherySpecificationDO> specification) {
        mallPeripherySpecificationMapper.batchInsertDO(specification);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByPerId(Long perId) {
        mallPeripherySpecificationMapper.deletedByPerId(perId);
    }

    @Override
    public MallPeripherySpecificationVO selectFirstByPeripheryId(Long itemId) {
        return  MallPeripherySpecificationDtoMapper.INSTANCE.do2VO(mallPeripherySpecificationMapper.selectFirstByPeripheryId(itemId));
    }

    @Override
    public void updateQuantity(Map<String, Long> quantityMap) {
        mallPeripherySpecificationMapper.updateQuantity(quantityMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByPerIds(String ids) {
        mallPeripherySpecificationMapper.deletedByPerIds(Convert.toStrArray(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addQuantity(Map<String, Long> map) {
        mallPeripherySpecificationMapper.addQuantity(map);
    }
}
